<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Moni
 *
 * @author MinhQuan
 */
class Moni {

    public function __constructor() {
        
    }

    public function loadMoni($datefrom, $dateto) {
        $db = Database::getInstance();
        $db = Database::getInstance();
        $user = User::getLogedInUser();
        $df = new DateTime($datefrom);
        $dt = new DateTime($dateto);
        $user = User::getLogedInUser();
        $datefrom = $df->format("Y-m-d");
        $dateto = $dt->format("Y-m-d");
        $query = "SELECT COUNT( messageid ) AS count, streamid, userscoring, fullname, DATE_FORMAT( timestamp,  '%b %d' ) AS DATE, timestamp
                    FROM sentimentdata
                    INNER JOIN users ON users.userid = sentimentdata.userscoring
                    WHERE userscoring >0 AND timestamp >= '$datefrom 00:00:00' AND timestamp <= '$dateto 23:59:59'
                    GROUP BY userscoring, DAY( timestamp ) 
                    ORDER BY userscoring, timestamp DESC ";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow > 0) {
                $monis = array();
                $messageIds = array();
                foreach ($result->dataTable->rows as $row) {
                    $moni = new self();
                    $moni->moniCount = $row['count'];
                    $moni->moniScore = $row['score'];
                    $moni->moniDateCreated = new DateTime($row['timestamp']);
                    $moni->moniUserFullname = $row['fullname'];
                    $monis[] = $moni;
                }
            }
            return $monis;
        } else {
            $controller = new ErrorController(new Error(), new ErrorView, $result->returnCode, $result->message);
            $controller->returnError();
            return NULL;
        }
    }

    public function reloadMoni($datefrom, $dateto, $userid) {
        $db = Database::getInstance();
        $df = new DateTime($datefrom);
        $dt = new DateTime($dateto);
        $user = User::getLogedInUser();
        $datefrom = $df->format("Y-m-d");
        $dateto = $dt->format("Y-m-d");
        if ($userid == 0) {
            $query = "SELECT COUNT( messageid ) AS count, streamid, userscoring, fullname, DATE_FORMAT( timestamp,  '%b %d' ) AS DATE, timestamp
                        FROM sentimentdata
                        INNER JOIN users ON users.userid = sentimentdata.userscoring
                        WHERE  userscoring>0 AND timestamp >= '$datefrom 00:00:00' AND timestamp <= '$dateto 23:59:59'
                        GROUP BY userscoring, DAY( timestamp ) 
                        ORDER BY timestamp DESC ";
        } elseif ($userid != 0) {
            $query = "SELECT COUNT( messageid ) AS count, fullname, DATE_FORMAT( timestamp,  '%b %d' ) AS DATE, timestamp
                        FROM sentimentdata
                        INNER JOIN users ON users.userid = sentimentdata.userscoring WHERE userscoring ='$userid' AND timestamp >= '$datefrom 00:00:00' AND timestamp <= '$dateto 23:59:59' GROUP BY DAY( timestamp ) ORDER BY timestamp DESC";
        }
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow > 0) {
                $monis = array();
                $messageIds = array();
                foreach ($result->dataTable->rows as $row) {
                    $moni = new self();
                    $moni->moniCount = $row['count'];
                    $moni->moniDateCreated = new DateTime($row['timestamp']);
                    $moni->moniUserFullname = $row['fullname'];
                    $monis[] = $moni;
                }
            }
            return $monis;
        } else {
            $controller = new ErrorController(new Error(), new ErrorView, $result->returnCode, $result->message);
            $controller->returnError();
            return NULL;
        }
    }

    public function monthU($userid) {
        $db = Database::getInstance();
        $user = User::getLogedInUser();
        $now = getdate();
        $y = $now["year"];
        $m = $now['mon'];
        if ($m != 2) {
            if ($m == 1 || $m == 3 || $m == 5 || $m == 7 || $m == 8 || $m == 10 || $m == 12) {
                $d = 31;
            } else {
                $d = 30;
            }
        } else {
            if ($y % 400 == 0) {
                $d = 29;
            } else {
                $d = 28;
            }
        }
        $datefrom = $y . '-' . $m . '-' . '01';
        $dateto = $y . '-' . $m . '-' . $d;
        if ($userid == 0) {
            $query = "SELECT COUNT( messageid ) AS count, fullname
                        FROM sentimentdata
                        INNER JOIN users ON users.userid = sentimentdata.userscoring
                        WHERE timestamp >=  '$datefrom 00:00:00'
                        AND timestamp <=  '$dateto 23:59:59'
                        GROUP BY userscoring ORDER BY count DESC";
        } else {
            $query = "SELECT COUNT( messageid ) AS count, fullname
                        FROM sentimentdata
                        INNER JOIN users ON users.userid = sentimentdata.userscoring
                        WHERE userscoring='$userid' AND timestamp >=  '$datefrom 00:00:00'
                        AND timestamp <=  '$dateto 23:59:59' ORDER BY count DESC";
        }
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow > 0) {
                $monis = array();
                $messageIds = array();
                foreach ($result->dataTable->rows as $row) {
                    $moni = new self();
                    $moni->moniCount = $row['count'];
                    $moni->moniUserFullname = $row['fullname'];
                    $monis[] = $moni;
                }
            }
            return $monis;
        } else {
            $controller = new ErrorController(new Error(), new ErrorView, $result->returnCode, $result->message);
            $controller->returnError();
            return NULL;
        }
    }

    public function lastUpdate($userid) {
        $db = Database::getInstance();
        if ($userid == 0)
            $query = "SELECT fullname, MAX( TIMESTAMP ) 
                    FROM sentimentdata
                    INNER JOIN users ON users.userid = sentimentdata.userscoring
                    GROUP BY userscoring ORDER BY MAX(timestamp) DESC";
        else
            $query = "SELECT fullname, MAX( TIMESTAMP ) 
                    FROM sentimentdata
                    INNER JOIN users ON users.userid = sentimentdata.userscoring
                    WHERE userscoring ='$userid' ORDER BY MAX(timestamp) DESC";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow > 0) {
                $monis = array();
                $messageIds = array();
                foreach ($result->dataTable->rows as $row) {
                    $moni = new self();
                    if ($row['1'] == '')
                        continue;
                    $moni->moniLast = $row['1'];
                    $moni->moniUserFullname = $row['fullname'];
                    $monis[] = $moni;
                }
            }
            return $monis;
        } else {
            $controller = new ErrorController(new Error(), new ErrorView, $result->returnCode, $result->message);
            $controller->returnError();
            return NULL;
        }
    }

}

?>
